<script setup>
import {formatTime} from "@/utils/format.js";
import {useRouter} from "vue-router";
import {wishlistAddAPI} from "@/api/FrontApi/user.js";
import {useUserStore} from "@/stores/index.js";
import 'element-plus/theme-chalk/el-message.css'
import {ElMessage} from "element-plus";

const props = defineProps({
  game: Object
})

// 游戏详情页
const router = useRouter()
const gameDetail = () => {
  router.push(`/store/detail/${props.game.id}`)
}

// 添加至愿望单
const userStore = useUserStore()
const addToWishlist = async (event) => {
  event.stopPropagation(); // 阻止事件冒泡
  if (userStore.token === '' || userStore.token === null) {
    // 未登录
    router.push('/login')
    return
  }
  const res = await wishlistAddAPI(props.game.id)
  if (res.code === 1) {
    ElMessage.success("添加成功")
  }
}
</script>

<template>
  <div class="game-container" @click="gameDetail">
    <div class="card">
      <div class="left">
        <img v-img-lazy="game.gameDetail.higherPicture">
        <span @click="addToWishlist($event)" class="add-wish-list"><i class="iconfont icon-shoucang"></i></span>
      </div>
      <div class="right">
        <div class="info">
          <div class="name ellipsis">{{game.chineseName}}({{ game.englishName }})</div>
          <div class="issue-date">发行日期：{{ formatTime(game.issueDate) }}</div>
          <div class="rate"><span>游戏评分:</span><span>{{ game.rate }}</span></div>
          <div class="category">
            <span v-for="item in game.category" :key="item">{{ item }}</span>
          </div>
          <div class="developer">开发商：{{ game.developer }}</div>
        </div>
        <div class="game-price">
          <template v-if="game.gamePrice.currentPrice === '0.00'">
            免费开玩
          </template>
          <template v-else>
            <template v-if="game.gamePrice.discount === 0">
              ￥{{ game.gamePrice.originPrice }}
            </template>
            <template v-else>
              <div class="discount">
                <div class="discount-percent">-{{game.gamePrice.discount}}%</div>
                <div class="discount-price">
                  <span class="discount-origin-price">￥{{ game.gamePrice.originPrice }}</span>
                  <span class="discount-current-price">￥{{ game.gamePrice.currentPrice }}</span>
                </div>
              </div>
            </template>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.game-container {
  cursor: pointer;
  box-shadow: 0 0 8px rgba(0, 0, 0, 1);
  .card {
    height: 380px;
    display: flex;
    background-color: rgba($textColor, 0.5);
    .left {
      position: relative;
      img {
        width: 300px;
        object-position: center;
        object-fit: cover;
        transition: transform 0.3s ease, box-shadow 0.3s ease;
        &:hover {
          transform: scale(102%);
          box-shadow: 0 0 8px rgba(0, 0, 0, 1);
        }
      }
      .add-wish-list {
        width: 34px;
        height: 34px;
        line-height: 34px;
        text-align: center;
        cursor: pointer;
        position: absolute;
        top: 8px;
        right: 8px;
        border-radius: 4px;
        background-color: #54a5d4;
        box-shadow: 0 0 8px rgba(0, 0, 0, 1);
        transition: all 0.1s;
        overflow: hidden;

        &:hover {
          width: 140px;
        }
        &:hover i {
          margin-left: 6px;
        }

        &:hover::after {
          margin: 0 8px;
          font-size: 14px;
          color: #fff;
          content: "添加至愿望单";
          text-shadow: 1px 1px 2px rgba(0, 0, 0, 1);
        }
        i {
          color: #fff;
          font-size: 18px;
        }
      }
    }
    .right {
      padding: 20px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .info {
        .name {
          margin-bottom: 10px;
          font-size: 30px;
          color: #fff;
        }
        .issue-date {
          margin-bottom: 10px;
          font-size: 18px;
          color: #c6d4df;
        }
        .rate {
          margin-bottom: 10px;
          font-size: 16px;
          color: #c6d4df;
          span:last-child {
            margin-left: 10px;
            color: $themeColor;
          }
        }
        .category {
          margin-bottom: 10px;
          color: #c6d4df;
          padding: 10px 0;
          span {
            border-radius: 4px;
            padding: 2px 10px;
            margin: 0 4px 4px 0;
            background-color: #c6d4df26;;
          }
        }
        .developer {
          font-size: 20px;
          color: #c6d4df;
        }
      }
      .game-price {
        color: #fff;
        font-size: 30px;
        .discount {
          height: 52px;
          display: flex;
          .discount-percent {
            line-height: 50px;
            text-align: center;
            padding: 0 4px;
            background-color: $discountBackgroundColor;
            color: $discountTextColor;
            text-shadow: 1px 1px 4px rgba(0, 0, 0, 1);
          }
          .discount-price {
            display: flex;
            flex-direction: column;
            text-align: center;
            background-color: #525252;
            .discount-origin-price {
              line-height: 24px;
              font-size: 16px;
              margin: 0 20px;
              position: relative;
              color: #738895;
              &:before {
                content: '';
                left: 0;
                right: 0;
                position: absolute;
                top: 43%;
                border-bottom: 1.5px solid #738895;
                transform: skewY(-8deg);
                box-shadow: 0 0 2px black;
              }
            }
            .discount-current-price {
              line-height: 28px;
              font-size: 20px;
              padding: 0 4px 0 0;
            }
          }
        }
      }
    }
  }
}
</style>
